<%--
  Created by IntelliJ IDEA.
  User: 小袁啊
  Date: 2024/10/16
  Time: 上午10:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <%--    引入vuejs和axios--%>
    <script src="/js/vue.js"></script>
    <script src="/js/axios.main.js"></script>
    <%--    引入Element--%>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app" align="center">
    <!-- Form -->
    <el-button type="primary"  @click="dialogFormVisible = true">快来添加吧</el-button>

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="姓名" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="age" :label-width="formLabelWidth">
                <el-input v-model="form.age" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="性别" :label-width="formLabelWidth">
                <el-input v-model="form.gender" autocomplete="off"></el-input>
            </el-form-item>
            <%--            <el-form-item label="生日" :label-width="formLabelWidth">--%>
            <%--                <el-date-picker--%>
            <%--                        v-model="form.birthday"--%>
            <%--                        type="date"--%>
            <%--                        placeholder="选择日期">--%>
            <%--                </el-date-picker>--%>
            <%--            </el-form-item>--%>
            <el-form-item label="生日" :label-width="formLabelWidth">
                <el-input v-model="form.birthday" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="头像" :label-width="formLabelWidth">
                <el-input v-model="form.headimg" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="班级id" :label-width="formLabelWidth">
                <el-input v-model="form.clazzId" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addStudent()">确 定</el-button>
        </div>
    </el-dialog>

    <el-table
    <%--        data表示数据源
    --%>
            :data="students"
            border
            style="width: 95%">
        <el-table-column
        <%--            prop表属性名
                        label显示的文字
                        width列宽
        --%>
                prop="id"
                label="编号">
        </el-table-column>
        <el-table-column
                prop="name"
                label="姓名">
        </el-table-column>
        <el-table-column
                prop="age"
                label="年龄">
        </el-table-column>
        <el-table-column
                prop="gender"
                label="性别">
        </el-table-column>
        <el-table-column
                prop="birthday"
                label="生日">
        </el-table-column>
        <el-table-column
                prop="clazz.name"
                label="班级">
        </el-table-column>
        <%--        头像--%>
        <el-table-column
                label="头像">
            <template slot-scope="scope">
                <img :src="scope.row.headimg" width="60px">
            </template>
        </el-table-column>
        <el-table-column
        <%--            label= 设置成超链接或者执行跳转方法，跳转其他页面--%>
                label="操作"
                wight="200px">
            <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" circle>
                    编辑
                </el-button>
                <el-button type="danger" icon="el-icon-delete" @click="deleteById(scope.row.id)">
                    删除
                    <i class="el-icon-upload el-icon--right"></i>
                </el-button>
            </template>
        </el-table-column>


    </el-table>




</div>

</body>

<script>
    let app = new Vue({
        el: '#app',
        data: {
            //定义一个变量，用来接收数据
            students:undefined,//不知道什么类型
            dialogFormVisible: false,
            formLabelWidth: '120px',
            form: {

                name: '',
                age: '',
                gender: '',
                birthday: '',
                headimg: '',
                clazzId: '',
            },
        },
        created() {
            //在页面加载的时候就发送请求，获取数据
            this.getAll();
        },
        methods: {
            //获取所有学生信息
            getAll() {
                axios.get("/student/getAll").then(resp => {
                    if (resp.data.code === 200) {
                        //把返回的数据赋值给students
                        this.students = resp.data.data;
                        console.log(resp);
                    } else {
                        alert(resp.data.msg);
                    }
                })
            },
            addStudent() {
                axios.post("/student/add", this.form).then(resp => {
                    console.log(resp);
                    if (resp.data.code === 200) {
                        //添加成功，重新加载数据
                        this.dialogFormVisible = false;
                        this.getAll();
                        this.$message({
                            message: '恭喜你，这是一条成功消息',
                            type: 'success'
                        });
                    } else {
                        alert(resp.data.msg);
                        this.$message.error(resp.data.msg);
                    }
                })
            },
            deleteById(id) {
                axios.delete("/student/delete?id=" + id).then(resp => {
                    //alert(resp.data.id);
                    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {//成功
                        //删除操作
                        if (resp.data.code === 200) {
                            //删除成功，重新加载数据
                            this.getAll();
                        } else {
                            alert(resp.data.msg);
                        }
                        //提示信息
                        this.$notify({
                            title: '成功',
                            message: resp.data.msg,
                            type: 'success'
                        });
                    }).catch(() => {//取消提示信息
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });

                })

            }
        }

    })
</script>
</html>
